package studio.mandysa.tuke

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import simon.tuke.livedata
import simon.tuke.value


class MainActivity : ComponentActivity() {

    private val mLiveData by livedata(0, "cookie")

    private var mValue by value(15, "value", isCache = true)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val livedata by mLiveData.observeAsState()
            Column {
                Text(text = title.toString(), fontSize = 20.sp)
                Spacer(modifier = Modifier.height(2.dp))
                Text(text = "$livedata")
                Button(onClick = { mLiveData.value = mLiveData.value!! + 1 }) {
                    Text(text = "+1")
                }
                Spacer(modifier = Modifier.height(20.dp))
                Text(text = "$mValue")
                Button(onClick = { mValue = mValue!! + 1 }) {
                    Text(text = "+1")
                }
                Button(onClick = { this@MainActivity.recreate() }) {
                    Text(text = "刷新")
                }
            }
        }
    }
}